package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Client;
import com.scau.service.impl.business.ClientService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class ClientAction extends BaseAction implements Serializable {
	private final static Log logger = LogFactory.getLog(ClientAction.class);
	private PageController pager = null;
	private Client client;
	private ClientService clientService = null;
	private List<Client> clientList = null;

	/**
	 * 列表显示
	 * 
	 * @return
	 */
	public String list() {
		int totalRows = clientService.getRecordCount(new Client());
		String URL=getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);		
		clientList = clientService.list(new Client(),
				this.pager.getPageStartRow(), pager.getPageSize(), null, null);
		pager.setData(clientList);		
		return "list";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String get() {
		client = clientService.get(client);
		return "edit";
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			clientService.save(client);
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("client", client);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String clientId : ids) {
			Client sp = new Client();
			if (null != clientId && !"0".equals(clientId)) {
				clientService.delete(sp, Long.parseLong(clientId));
			}
		}
		return list();
	}

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public ClientService getClientService() {
		return clientService;
	}

	@Resource
	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public List<Client> getClientList() {
		return clientList;
	}

	public void setClientList(List<Client> clientList) {
		this.clientList = clientList;
	}

}
